package org.ladybug.web.img;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class ImgServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String uri = req.getRequestURI();
		String context = req.getContextPath();
		uri = uri.substring(uri.lastIndexOf(context) + context.length() + 1);
		uri = new StringBuffer("/WEB-INF/" + uri).toString();
		System.out.println("ImgServlet.doGet() --> img URI: " + uri);
		InputStream inStream = getServletContext().getResourceAsStream(uri);
		if (inStream != null) {
			OutputStream out = resp.getOutputStream();
			out.write(IOUtils.toByteArray(inStream));
			out.flush();
			out.close();
		}
	}

}
